package com.zhupf.stack;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author zhupf
 * @date 2024年12月09日 9:21
 * @Description 946. 验证栈序列
 */
public class ValidateStackSequences {
    public static void main(String[] args) {
        int[] pushed = {4,0,3,1,2};
        int[] popped = {3,1,0,2,4};
        System.out.println(validateStackSequences(pushed, popped));
    }

    public static boolean validateStackSequences(int[] pushed, int[] popped) {
        Deque<Integer> deque = new LinkedList<>();
        int len = pushed.length;
        int j = 0;
        for (int i = 0; i < len; i++) {
            deque.push(pushed[i]);
            while ( !deque.isEmpty() && deque.peek() == popped[j]) {
                deque.pop();
                j++;
            }
        }

        return true;
    }
}
